import React, { Component } from 'react';
import { messages } from "share/common";
import { Form, Button, message, Spin, Checkbox } from 'antd';
import autoAuditService from "./auto-audit.service";
const FormItem = Form.Item;

class AutoAuditSetting extends Component {

  state = {
    saving: false
  };

  componentDidMount() {
    const { data } = this.props;
    const { setFieldsValue } = this.props.form;
    let values = this.pick(data, ['enableAuto', 'enableManual']);
    setFieldsValue(values);
  }

  handleBack = () => {
    this.props.close();
  };

  handleSave = e => {
    e.preventDefault();
    const { form, data, onSaveSetting } = this.props;
    form.validateFieldsAndScroll((err, values) => {
      if(!err) {
        this.setState({ saving: true });
        autoAuditService.updateAutoSetting({ ...data, ...values })
          .then(res => {
            message.success(messages('common.operate.success')/*操作成功*/);
            onSaveSetting(res.data);
            this.props.close();
          })
          .finally(() => {
            this.setState({ saving: false });
            this.props.close(true);
          })
      }
    })
  };

  render() {
    const { getFieldDecorator } = this.props.form;
    const formItemLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 14 },
      },
    };
    const { saving } = this.state;
    return (
      <div className='auto-audit-setting'>
        <Form className='new-form' onSubmit={this.handleSave}>
          <FormItem
            label={messages('expense-14.key458')/*调用配置*/}
            {...formItemLayout}
            extra={messages('expense-14.key454')/*手动调用即支持在审核界面通过点击自动审核进行规则调用校验*/}
          >
            {getFieldDecorator('enableAuto', {
              valuePropName: 'checked'
            })(
              <Checkbox>{messages('expense-14.key455')/*审批通过后自动调用*/}</Checkbox>
            )}
            {getFieldDecorator('enableManual', {
              valuePropName: 'checked'
            })(
              <Checkbox>{messages('expense-14.key456')/*人工审核时手动调用*/}</Checkbox>
            )}
          </FormItem>
          <div className='slide-footer'>
            <Button
              type='primary'
              htmlType='submit'
              loading={saving}
            >
              {messages('common.save')/*保存*/}
            </Button>
            <Button onClick={this.handleBack}>{messages('common.back')/*返回*/}</Button>
          </div>
        </Form>
      </div>
    )
  }
}

const WrappedAutoAuditSetting = Form.create()(AutoAuditSetting);

export default WrappedAutoAuditSetting

